package top.wenzz.commons.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import top.wenzz.commons.entity.LoginUser;

import java.util.HashMap;
import java.util.Map;

/**
 * Spring Security工具类
 * Created by cui-fate on 2017/7/16.
 */
public class SpringSecurityUtils {
    /**
     * 获取当前登录用户信息
     */
    public static LoginUser getUserByContext() {
        Authentication a = SecurityContextHolder.getContext().getAuthentication();
        return a != null && a.getPrincipal() instanceof LoginUser ? (LoginUser) a.getPrincipal() : LoginUser.builder().build();
    }

    public static Map<String, Object> getWebUserData() {
        LoginUser user = getUserByContext();
        Map<String, Object> retData = new HashMap<>();
        retData.put("username", user.getUsername());
        retData.put("sns", user.getPermissions());
        retData.put("menus", user.getMenus());
        return retData;
    }
}
